#include "Request.h"

#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <errno.h>
#include <curl/curl.h>

Request::Request(string url)
{
    this->url = url;
}

Request::~Request()
{
}

static size_t readdata(void *ptr, size_t size, size_t nmemb, void *stream)
{
    string *result = (string *) stream;

    result->append((char *) ptr, 0, size * nmemb);

    return nmemb * size;
}

string Request::GetResponse()
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if (curl) {
	string result;
	curl_easy_setopt(curl, CURLOPT_URL, url.data());
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readdata);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
	res = curl_easy_perform(curl);
	curl_easy_cleanup(curl);

	return result;
    } else
	return "";
}
